<?php

namespace App\Http\Controllers\Admin\Content;

use App\Http\Controllers\AdminController;
use App\Models\Content\Adv;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AdvController extends AdminController
{

    public function data(Request $request)
    {
        $model = Adv::query()->select(['id', 'sort', 'name', 'thumb', 'status', 'created_at', 'updated_at']);
        if ($keywords = $request->get('keywords')) {
            $model->where('name', 'like', $keywords.'%');
        }
        $total = $model->count();
        $page = (int)$request->get('page', 1);
        $pageSize = (int)$request->get('pageSize', 10);
        $list = $model->orderBy('id', 'desc')->offset(($page-1)*$pageSize)->limit($pageSize)->get();
        return $this->succeed([
            'total' => $total,
            'rows'  => $list,
            'server' => $request->server(),
        ]);
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => ['required','max:50'],
            'redirect_url' => ['nullable','active_url']
        ]);
        $data = $request->only(['sort', 'name', 'thumb', 'redirect_url', 'status']);
        DB::beginTransaction();
        try {
            $model = Adv::query()->create([
                'name' => $data['name'],
                'thumb' => (string)$data['thumb'],
                'redirect_url' => (string)$data['redirect_url'],
                'sort' => (int)$data['sort'],
                'status' => (int)$data['status'],
            ]);
            $this->createSyslog('添加了一条广告记录', $data, $model);
            DB::commit();
            return $this->succeed([
                'id' => $model['id'],
                'created_at' => $model['created_at'],
                'updated_at' => $model['updated_at']
            ]);
        }catch (\Exception) {
            DB::rollBack();
        }
        return $this->fail();
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'name' => ['required','max:50'],
            'redirect_url' => ['nullable','active_url']
        ]);
        $model = Adv::query()->findOrFail($id);
        $data = $request->only(['sort', 'name', 'thumb', 'redirect_url', 'status']);
        DB::transaction(function () use ($model, $data) {
            $model->update([
                'name' => $data['name'],
                'thumb' => (string)$data['thumb'],
                'redirect_url' => (string)$data['redirect_url'],
                'sort' => (int)$data['sort'],
                'status' => (int)$data['status'],
            ]);
            $this->createSyslog('更新了一条广告记录', $data, $model);
        });
        return $this->succeed();
    }

    public function destroy(Request $request)
    {
        $request->validate([
            'ids' => ['required','array','max:'.static::QUERY_LIMIT]
        ]);
        $ids = $request->get('ids');
        DB::transaction(function () use ($ids) {
            $rows = Adv::query()->whereIn('id', $ids)->limit(static::QUERY_LIMIT)->delete();
            $this->createSyslog('批量删除了'.$rows.'条广告记录', ['ids' => implode(',', $ids)]);
        });
        return $this->succeed();
    }
}
